#include "data_mysql.hpp"
#include "user.hxx"
#include "user-odb.hxx"
#include "logger.hpp"
#include <gflags/gflags.h>

DEFINE_string(host, "120.76.98.215", "这是MySQL服务器的地址");
DEFINE_int32(port, 1314, "这是MySQL服务器的默认端口号");
DEFINE_int32(max_pool, 3, "这是MySQL连接池的最大连接数");
DEFINE_string(user, "zhiang_root", "这是登录用户名");
DEFINE_string(password, "123456Aa.", "这是登录用户密码");
DEFINE_string(db, "chatSystem", "这是数据库的库");
DEFINE_string(charset, "utf8", "这是数据库访问的编码格式");

DEFINE_bool(mode, false, "运行模式： true-发布模式； false-测试模式");
DEFINE_string(file, "", "发布模式日志更新地址");
DEFINE_int32(level, 1, "默认日志等级");

void insert(zhiang::UserTable& user_table) {
    auto user1 = std::make_shared<User>("user1", "zhiang", "123456");
    auto user2 = std::make_shared<User>("user2", "11122223333");
    user_table.insert(user1);
    user_table.insert(user2);
}

void update_nickname(zhiang::UserTable& user_table) {
    auto user = user_table.select_by_nickname("zhiang");
    user->nickname("ZHIANG");
    user_table.update(user);
}

void update_password(zhiang::UserTable& user_table) {
    auto user = user_table.select_by_id("user2");
    user->password("123456");
    user_table.update(user);
}

void update_description(zhiang::UserTable& user_table) {
    auto user = user_table.select_by_phone("11122223333");
    user->description("我要成为大摄影师，我要幸福！！！");
    user_table.update(user);
}

void select_all_name(zhiang::UserTable& user_table) {
    std::vector<std::string> vs = {"user1", "user2"};
    std::vector<User> vu = user_table.select_multi_users(vs);
    for(auto& user : vu) {
        std::cout << "nickname->" << user.nickname() << std::endl;
    }
}

int main(int argc, char* argv[]) {
    google::ParseCommandLineFlags(&argc, &argv, true);
    init_logger(FLAGS_mode, FLAGS_file, FLAGS_level);
    std::shared_ptr<odb::core::database> db = zhiang::ODBFactory::create(
        FLAGS_user, FLAGS_password, FLAGS_db, FLAGS_host, FLAGS_port, FLAGS_charset, FLAGS_max_pool);
    zhiang::UserTable user_table(db);
    // insert(user_table);
    // update_nickname(user_table);
    // update_password(user_table);
    // update_description(user_table);
    select_all_name(user_table);
    return 0;
}
